import redis.asyncio as redis
from app.core.config import settings


class RedisManager:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance.init_redis()
        return cls._instance

    def init_redis(self):
        self.redis_pool = redis.ConnectionPool.from_url(
            settings.REDIS_URL,
            max_connections=20,
            decode_responses=True
        )

    async def get_redis(self):
        return redis.Redis(connection_pool=self.redis_pool)

    async def close(self):
        await self.redis_pool.disconnect()


# 全局 Redis 管理器
redis_manager = RedisManager()